package com.gmzg.gmzgoffice.works;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.test.mock.MockApplication;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmzg.gmzgoffice.GMZGApplication;
import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Attendance;
import com.prolificinteractive.materialcalendarview.CalendarDay;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignDetailActivity extends AppCompatActivity {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.time_tv)
    TextView mTimeTv;
    @BindView(R.id.place_tv)
    TextView mPlaceTv;
    @BindView(R.id.remark_et)
    EditText mRemarkEt;
    @BindView(R.id.company_tv)
    TextView mCompanyTv;
    @BindView(R.id.submit)
    Button mSubmit;
    private String mPoiname;
    private String mAddress;
    private long mTime;
    private String mCompany;
    private Date mDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_detail);
        ButterKnife.bind(this);
        mToolbarTitle.setText("签到");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

        initView();


    }

    private void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        mPoiname = bundle.getString(SignActivity.POINAME);
        mAddress = bundle.getString(SignActivity.ADDRESS);
        mTime = bundle.getLong(SignActivity.TIME);
        mCompany = bundle.getString(SignActivity.COMPANY);
        mPlaceTv.setText(mPoiname + " " + mAddress);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        mDate = new Date(mTime);
        mTimeTv.setText(df.format(mDate));
        mCompanyTv.setText(mCompany);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick(R.id.submit)
    public void onClick() {
        String remark = mRemarkEt.getText().toString().trim();
        if (!TextUtils.isEmpty(remark)) {
            final Attendance attendance = new Attendance();
            attendance.setCome_time(mDate);
            attendance.setPosition(mAddress);
            String userName = ((GMZGApplication) getApplication()).getUserName();
            if (userName != null) {
                attendance.setUser_name(userName);
            }
            new AlertDialog.Builder(this)
                    .setTitle("签到")
                    .setMessage("确定要提交数据吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            attendance.save(new SaveListener<String>() {
                                @Override
                                public void done(String objectId, BmobException e) {
                                    if(e==null){
                                        SharedPreferences nums = getSharedPreferences("signnum", MODE_PRIVATE);
                                        SharedPreferences.Editor editor = nums.edit();
                                        int num = nums.getInt("num", 0);
                                        editor.putInt("num",num++);
                                        editor.apply();
                                        setResult(RESULT_OK);
                                        Log.d("aaaa", "onActivityResult详细界面: "+nums.getInt("num", 0));
                                        finish();
                                    }else{
                                        Toast.makeText(SignDetailActivity.this, "添加数据失败，请检查网络", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .show();
        }else {
            Toast.makeText(this, "备注信息不能为空", Toast.LENGTH_SHORT).show();
        }
    }
}
